// src/lib/http.js
const syncRequest = require('sync-request');

function request(method, url, options = {}) {
  const { headers = {}, body = null, timeout = 5000 } = options;

  try {
    const res = syncRequest(method, url, {
      headers,
      body,
      timeout
    });

    if (res.statusCode >= 200 && res.statusCode < 300) {
      return res.getBody('utf8').toString();
    } else {
      throw new Error(`Request failed with status code: ${res.statusCode}`);
    }
  } catch (error) {
    console.error('HTTP request failed:', error.message);
    throw error;
  }
}

function get(url, options) {
  return request('GET', url, options);
}

function post(url, options) {
  return request('POST', url, options);
}

module.exports = {
  get,
  post
};